package cn.edu.scau.cmil.shishuai.comprehensive.address.view;

import java.io.File;

import javax.swing.JOptionPane;

import cn.edu.scau.cmil.shishuai.comprehensive.address.MainApp;
import cn.edu.scau.cmil.shishuai.comprehensive.address.model.Person;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class StudentSignController {
	@FXML
	private Button btn_cancel;
	
	@FXML
	private Button btn_OK;

	@FXML
	private ToggleButton tog_teabtn;
	
	@FXML
	private javafx.scene.control.Label lab_user;
	
	@FXML
	private javafx.scene.control.Label lab_class;

	@FXML
	private javafx.scene.control.Label lab_profess;
	
	@FXML
	private TextField text_class;
	
	@FXML
	private TextField text_profess;
	
	@FXML
	private TextField text_user;
	
	@FXML
	private TextField text_password;
	
	@FXML
	private TextField text_name;
	
	MainApp mainApp = new MainApp();
			
	public void onToggleButtonClick() {
		
		if(tog_teabtn.selectedProperty().getValue()) {
			tog_teabtn.setText("切换学生注册");
			lab_user.setText("工号：");
			lab_class.setVisible(false);
			lab_profess.setVisible(false);
			text_class.setVisible(false);
			text_profess.setVisible(false);
			
		}
		else {
			tog_teabtn.setText("切换教师注册");
			lab_user.setText("学号：");
			lab_class.setVisible(true);
			lab_profess.setVisible(true);
			text_class.setVisible(true);
			text_profess.setVisible(true);
		}

	}
	
	public void onOKButtonOn(ActionEvent event) {
		String user = text_user.getText();
		String password = text_password.getText();
		String name = text_name.getText();
		String profess = text_profess.getText();
		String classroom = text_class.getText();
		if(!user.isEmpty() && !password.isEmpty() && !name.isEmpty()) {
			if(tog_teabtn.isSelected()) {
				mainApp.addPersonData(new Person(user, password, name, profess, classroom,"T"));
					}
			else {
				mainApp.addPersonData(new Person(user, password, name, profess, classroom,"S"));

			}
		
			handleSave();
			
	    	JOptionPane.showMessageDialog(null, "注册成功", "提示", JOptionPane.NO_OPTION);
			

	        
	    	onCancelButtonOn(event);

		}else {
	    	JOptionPane.showMessageDialog(null, "请完整填写信息", "错误", JOptionPane.ERROR_MESSAGE);
		}

        } 

	
	
	public void onCancelButtonOn(ActionEvent event) {
		Stage stage = (Stage)btn_cancel.getScene().getWindow();
	    stage.close();
	
	}
	@FXML
	private void initialize() {
		File personFile = mainApp.getPersonFilePath();
        if (personFile != null) {
        mainApp.loadPersonDataFromFile(personFile);
        } 


		text_user.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if(newValue.matches("[0-9]{0,20}")!=true) text_user.setText(oldValue);;
				
			}
		});
		
	}
	 @FXML
	    private void handleNew() {
	        mainApp.getPersonData().clear();
	        mainApp.setPersonFilePath(null);
	    }

	    /**
	     * Opens a FileChooser to let the user select an address book to load.
	     */
	    @FXML
	    private void handleOpen() {
	        FileChooser fileChooser = new FileChooser();

	        // Set extension filter
	        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
	                "XML files (*.xml)", "*.xml");
	        fileChooser.getExtensionFilters().add(extFilter);

	        // Show save file dialog
	        File file = fileChooser.showOpenDialog(mainApp.getPrimaryStage());

	        if (file != null) {
	            mainApp.loadPersonDataFromFile(file);
	        }
	    }

	    /**
	     * Saves the file to the person file that is currently open. If there is no
	     * open file, the "save as" dialog is shown.
	     */
	    @FXML
	    public void handleSave() {
	        File personFile = mainApp.getPersonFilePath();
	        if (personFile != null) {
	            mainApp.savePersonDataToFile(personFile);
	        } else {
	            handleSaveAs();
	        }
	    }

	    /**
	     * Opens a FileChooser to let the user select a file to save to.
	     */
	    @FXML
	    private void handleSaveAs() {
	        FileChooser fileChooser = new FileChooser();

	        // Set extension filter
	        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
	                "XML files (*.xml)", "*.xml");
	        fileChooser.getExtensionFilters().add(extFilter);

	        // Show save file dialog
	        File file = fileChooser.showSaveDialog(mainApp.getPrimaryStage());

	        if (file != null) {
	            // Make sure it has the correct extension
	            if (!file.getPath().endsWith(".xml")) {
	                file = new File(file.getPath() + ".xml");
	            }
	            mainApp.savePersonDataToFile(file);
	        }
	    }
	    /**
	     * Closes the application.
	     */
	    @FXML
	    private void handleExit() {
	        System.exit(0);
	    }


}
